package m.jclecai.com.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import m.jclecai.com.R;
import m.jclecai.com.inter.OnItemClick;

/**
 * Created by hp on 2017/4/27.
 * <p>
 * 标题下拉框适配器
 */

public class SelectBoxKSAdapter extends BaseAdapter implements View.OnClickListener {

    private final Context context;
    private final OnItemClick i;
    private int[] images;
    private String[] bonus;
    private String[] modes;
    private int flag;

    public SelectBoxKSAdapter(Context context, OnItemClick i) {
        this.context = context;
        this.i = i;
    }

    public void setData(String[] modes, String[] bonus, int[] images) {
        this.modes = modes;
        this.bonus = bonus;
        this.images = images;
    }

    public void setFlag(int flag) {
        this.flag = flag;
        i.onItemClick(String.valueOf(flag));
    }

    @Override
    public int getCount() {
        return modes.length;
    }

    @Override
    public Object getItem(int position) {
        return modes[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.select_box_ks_item, null);
            holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
            holder.tv_bonus = (TextView) convertView.findViewById(R.id.tv_bonus);
            holder.iv_image_one = (ImageView) convertView.findViewById(R.id.iv_image_one);
            holder.iv_image_two = (ImageView) convertView.findViewById(R.id.iv_image_two);
            holder.iv_image_three = (ImageView) convertView.findViewById(R.id.iv_image_three);
            holder.tv_add_one = (TextView) convertView.findViewById(R.id.tv_add_one);
            holder.tv_add_two = (TextView) convertView.findViewById(R.id.tv_add_two);
            holder.rl_mode = (RelativeLayout) convertView.findViewById(R.id.rl_mode);
            holder.rl_mode.setOnClickListener(this);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.position = position;
        holder.rl_mode.setTag(holder);
        holder.tv_mode.setText(modes[position]);
        holder.tv_bonus.setText(bonus[position]);

        if (position == 0) {
            holder.iv_image_one.setBackgroundResource(images[0]);
            holder.iv_image_two.setBackgroundResource(images[1]);
            holder.iv_image_three.setBackgroundResource(images[2]);
        } else if (position == 1) {
            holder.tv_add_one.setVisibility(View.GONE);
            holder.tv_add_two.setVisibility(View.GONE);
            holder.iv_image_one.setBackgroundResource(images[0]);
            holder.iv_image_two.setBackgroundResource(images[0]);
            holder.iv_image_three.setBackgroundResource(images[0]);
        } else if (position == 2) {
            holder.tv_add_one.setVisibility(View.GONE);
            holder.tv_add_two.setVisibility(View.GONE);
            holder.iv_image_one.setBackgroundResource(images[0]);
            holder.iv_image_two.setBackgroundResource(images[0]);
            holder.iv_image_three.setBackgroundResource(images[1]);
        } else if (position == 3) {
            holder.tv_add_one.setVisibility(View.GONE);
            holder.tv_add_two.setVisibility(View.GONE);
            holder.iv_image_one.setBackgroundResource(images[0]);
            holder.iv_image_two.setBackgroundResource(images[1]);
            holder.iv_image_three.setBackgroundResource(images[2]);
        } else if (position == 4) {
            holder.tv_add_one.setVisibility(View.GONE);
            holder.tv_add_two.setVisibility(View.GONE);
            holder.iv_image_one.setBackgroundResource(images[0]);
            holder.iv_image_two.setBackgroundResource(images[1]);
            holder.iv_image_three.setVisibility(View.GONE);
        }

        if (position == flag) {
            Log.i("一一一一一一一一一", "回调");
            holder.rl_mode.setBackgroundResource(R.drawable.shape_box_ks_down);
        } else {
            holder.rl_mode.setBackgroundResource(R.drawable.shape_box_ks_up);
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        ViewHolder holder = (ViewHolder) v.getTag();
        flag = holder.position;
        i.onItemClick(String.valueOf(flag));
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView tv_mode;
        TextView tv_bonus;
        ImageView iv_image_one;
        ImageView iv_image_two;
        ImageView iv_image_three;
        TextView tv_add_one;
        TextView tv_add_two;
        RelativeLayout rl_mode;
        int position;
    }
}
